package com.gky.gkytemplate.common.result;

import com.gky.gkytemplate.common.enums.ErrorCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    /**
     * 错误码
     */
    private int code;

    /**
     * 错误消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setMessage("ok");
        result.setCode(200);
        return result;
    }

    public static <T> Result<T> ok(T obj) {
        Result<T> result = new Result<>();
        result.setData(obj);
        result.setMessage("ok");
        result.setCode(200);
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        Result<T> result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        return result;
    }

    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
